<?php

namespace App\Admin;

use App\Models\AdminMenu;
use App\Models\AdminNode;
use App\Models\AdminRole;
use App\Models\AdminUser;
use App\Utils\Code;
use Illuminate\Http\Request;

class Authority extends BaseController
{

    function __construct()
    {
        parent::__construct();
        $this -> adminRole = new AdminRole;
        $this -> adminMenu = new AdminMenu;
        $this -> adminNode = new AdminNode;
    }
  

    /**
     * 角色列表
     */
    public function roleList(Request $request)
    {
        $param = $request->input();
        $result = $this->adminRole->list($param);
        if ($result) return renderData($result);
        return renderError(Code::FAILED_TO_FIND_DATA);
    }

    /**
     * 添加角色
     */
    public function addRole(Request $request)
    {
        $rules  = [
            'role_name'     => 'required|unique:admin_role',
            'menu_ids'      => 'nullable',
        ];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $data = $this -> validate($request, $rules);
        $result = $this -> adminRole -> insert($data);
        if ($result) return renderSuccess('添加成功');
        return renderError(Code::ADD_FAILED);

    }

    /**
     * 修改角色
     */
    public function updateRole(Request $request)
    {
        $rules  = [
            'id'            => 'required',
            'role_name'     => 'unique:admin_role,role_name,'.$request->input('id', 0).',id',
            'menu_ids'      => 'nullable',
        ];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $data = $this -> validate($request, $rules);
        $id = $data['id'];
        unset($data['id']);
        $result = $this -> adminRole -> where(['id' => $id]) -> update($data);
        if ($result) return renderSuccess('修改成功');
        return renderError(Code::UPDATE_FAILED);

    }

    /**
     * 删除管理员
     */
    public function deleteRole(Request $request)
    {
        $rules = ['id'  => 'required'];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $id = $request -> input('id');
        $result = $this -> adminRole -> where(['id' => $id]) -> delete();
        if ($result) return renderSuccess('删除成功');
        return renderError(Code::DELETE_FAILED);
    }

    /**
     * 菜单列表
     */
    public function menuList(Request $request)
    {
        $param = $request->input();
        $result = $this->adminMenu->list($param);
        if ($result) return renderData($result);
        return renderError(Code::FAILED_TO_FIND_DATA);
    }

    /**
     * 添加菜单
     */
    public function addMenu(Request $request)
    {
        $rules  = [
            'menu_name'     => 'required|unique:admin_menu',
            'path'          => 'nullable',
            'pid'           => 'required',
            'node_ids'      => 'nullable',
            'sort'          => 'nullable'
        ];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $data = $this -> validate($request, $rules);
        $result = $this -> adminMenu -> insert($data);
        if ($result) return renderSuccess('添加成功');
        return renderError(Code::ADD_FAILED);

    }

    /**
     * 修改菜单
     */
    public function updateMenu(Request $request)
    {
        $rules  = [
            'id'            => 'required',
            'menu_name'     => 'unique:admin_menu,menu_name,'.$request->input('id', 0).',id',
            'path'          => 'nullable',
            'pid'           => 'nullable',
            'node_ids'      => 'nullable',
            'sort'          => 'nullable'
        ];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $data = $this -> validate($request, $rules);
        $id = $data['id'];
        unset($data['id']);
        $result = $this -> adminMenu -> where(['id' => $id]) -> update($data);
        if ($result) return renderSuccess('修改成功');
        return renderError(Code::UPDATE_FAILED);

    }

    /**
     * 删除菜单
     */
    public function deleteMenu(Request $request)
    {
        $rules = ['id'  => 'required'];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $id = $request -> input('id');
        $result = $this -> adminMenu -> where(['id' => $id]) -> delete();
        if ($result) return renderSuccess('删除成功');
        return renderError(Code::DELETE_FAILED);
    }

    /**
     * 节点列表
     */
    public function nodeList(Request $request)
    {
        $param = $request->input();
        $result = $this->adminNode->list($param);
        if ($result) return renderData($result);
        return renderError(Code::FAILED_TO_FIND_DATA);
    }

    /**
     * 添加节点
     */
    public function addNode(Request $request)
    {
        $rules  = [
            'node_name'     => 'required|unique:admin_node',
            'path'          => 'nullable',
        ];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $data = $this -> validate($request, $rules);
        $result = $this -> adminNode -> insert($data);
        if ($result) return renderSuccess('添加成功');
        return renderError(Code::ADD_FAILED);

    }

    /**
     * 修改节点
     */
    public function updateNode(Request $request)
    {
        $rules  = [
            'id'            => 'required',
            'node_name'     => 'unique:admin_node,node_name,'.$request->input('id', 0).',id',
            'path'          => 'nullable',
        ];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $data = $this -> validate($request, $rules);
        $id = $data['id'];
        unset($data['id']);
        $result = $this -> adminNode -> where(['id' => $id]) -> update($data);
        if ($result) return renderSuccess('修改成功');
        return renderError(Code::UPDATE_FAILED);

    }

    /**
     * 删除节点
     */
    public function deleteNode(Request $request)
    {
        $rules = ['id'  => 'required'];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $id = $request -> input('id');
        $result = $this -> adminNode -> where(['id' => $id]) -> delete();
        if ($result) return renderSuccess('删除成功');
        return renderError(Code::DELETE_FAILED);
    }

}